#!/bin/sh
# https://github.com/relan/exfat

. ../../func

VER=1.2.8
URL=https://github.com/relan/exfat/releases/download/v${VER}
SRC=fuse-exfat
PKG=fuse-exfat_static
COMP=tar.gz
DESC=""
DEPS=
CAT=BuildingBlock
CWD=`pwd`

#libfuse_ver=3.0.0 #can't mount - segfaults
libfuse_ver=2.9.7

build_libfuse() {
	extract fuse-${libfuse_ver}.tar.gz
	cd fuse-${libfuse_ver}
	opts="--prefix=${libfuse_dir}
--disable-test
--disable-util
--disable-shared
--enable-static
"
	_configure
	_make ${MKFLG} LDFLAGS=-static CFLAGS=\"$(_cflags)\"
	_make install
	cd ..
}

build() {
	libfuse_dir=${PWD}/libfuse
	build_libfuse
	cd ${SRC}-${VER}
	#export FUSE_LIBS=${libfuse_dir}/lib/libfuse3.a
	export FUSE_LIBS=${libfuse_dir}/lib/libfuse.a
	opts="--prefix=/usr"
	_configure
	_make ${MKFLG} LDFLAGS=-static
	abort_if_file_not_found fuse/mount.exfat-fuse
	install -d -m 0755 $CWD/${PKG}-${VER}-${ARCH}/bin
	_install fuse/mount.exfat-fuse $CWD/${PKG}-${VER}-${ARCH}/bin
	cd -
	rm -rf fuse-${libfuse_ver}
	rm -rf libfuse
}

package() {
	# add this recipe
	install -d -m 0755 ./${PKG}-${VER}-${ARCH}/build
	cat ${PKG}.petbuild > ./${PKG}-${VER}-${ARCH}/build/$PKG-build-recipe
	# move, don't package
	cp -a --remove-destination ./${PKG}-${VER}-${ARCH}/* ../../00_${ARCH}
	rm -r ${SRC}-${VER}
	rm -r ${PKG}-${VER}-${ARCH}
	echo "moved to initrd"
	echo "done!"
}

# main
retrieve "${SRC}-${VER}.${COMP}"
retrieve https://github.com/libfuse/libfuse/releases/download/fuse-${libfuse_ver}/fuse-${libfuse_ver}.tar.gz
[ "$DLD_ONLY" = "yes" ] && exit
extract ${SRC}-${VER}.${COMP}
build
package

